package com.csglxt.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;

/**
 * 
 * @ClassName: DateConverter
 * @Description: 日期格式转换器
 * @author Itchbeilef
 * @date 2015-5-28 下午11:55:11
 * 
 */
public class DateConverter extends DefaultTypeConverter {
    @SuppressWarnings("rawtypes")
    @Override
    public Object convertValue(Map<String, Object> context, Object value,
            Class toType) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        try {
            if (toType == Date.class) { // 当字符串向Date类型转换时
                String[] params = (String[]) value;
                return sdf.parseObject(params[0]);
            } else if (toType == String.class) { // 当Date转换成字符串
                Date date = (Date) value;
                return sdf.format(date);
            }
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
